﻿//Copyright 2014 Thomas Rega

//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at

//http://www.apache.org/licenses/LICENSE-2.0

//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;

namespace TinyDeduplicator
{
    internal class DeletionManager
    {
        private IList files;
        private IDictionary<string, DirectoryInfo> parentDirectories = new Dictionary<string, DirectoryInfo>();

        internal DeletionManager(IList files)
        {
            this.files = files;
        }

        internal bool containsFiles()
        {
            return (object)files == null || files.Count == 0;
        }

        internal bool containsEmptyDirectories()
        {
            return parentDirectories.Count > 0;
        }

        internal bool doFilesExist(out string path)
        {
            path = string.Empty;
            bool filesExist = true;

            foreach (DisplayFile file in files)
            {
                path = file.path;

                if (!File.Exists(path))
                {
                    filesExist = false;
                    break;
                }
            }

            return filesExist;
        }

        internal string filesDisplayName
        {
            get
            {
                string displayName;

                if (files.Count == 1)
                {
                    DisplayFile file = (DisplayFile)files[0];

                    displayName = file.path;
                }
                else
                {
                    displayName = files.Count + " files";
                }

                return displayName;
            }
        }

        internal string directoriesDisplayName
        {
            get
            {
                string displayName;

                if (parentDirectories.Count == 1)
                {
                    displayName = parentDirectories.Keys.First();
                }
                else
                {
                    displayName = parentDirectories.Count + " empty parent directories";
                }

                return displayName;
            }
        }

        internal void recycleFiles()
        {
            foreach (DisplayFile file in files)
            {
                try
                {
                    FileSystem.DeleteFile(file.path, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                    file.deleted = true;
                    file.selected = false;
                }
                catch (OperationCanceledException e) { }
            }

            foreach (DisplayFile file in files)
            {
                FileInfo fileInfo = new FileInfo(file.path);
                DirectoryInfo directoryInfo = fileInfo.Directory;
                string fullName = directoryInfo.FullName;

                if (!parentDirectories.ContainsKey(fullName))
                {
                    if (directoryInfo.GetFiles().Length == 0 && directoryInfo.GetDirectories().Length == 0)
                    {
                        parentDirectories.Add(fullName, directoryInfo);
                    }
                }
            }
        }

        internal void recycleEmptyDirectories()
        {
            foreach (DirectoryInfo directory in parentDirectories.Values)
            {
                FileSystem.DeleteDirectory(directory.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
            }
        }

        internal void openFileLocations()
        {
            foreach (DisplayFile file in files)
            {
                string process = Environment.SystemDirectory.Substring(0, Environment.SystemDirectory.LastIndexOf('\\') + 1) + "explorer.exe";
                Process.Start(process, "/select,\"" + file.path + "\"");

                //When opening multiple simultaneous instances of explorer, sometimes Windows will not select the correct file.
                //This sucks, but we will use sleep to stagger the opening of explorer instances. -trega
                Thread.Sleep(50);
            }
        }
    }
}
